#include "manageface.h"

manageFace::manageFace(QWidget *parent) : QWidget(parent)
{
    //this->setFixedSize(1200,700);

    // 去掉标题栏
    // this->setWindowFlags(Qt::FramelessWindowHint);
    // this->setMinimumSize(1300,1000);
    this->setMinimumSize(1300,800);
    this->setWindowTitle("IMA医疗预约系统");

    oldPos = QPoint(0, 0);
    Init();
}


void  manageFace::Init ()
{
    qDebug()<<__FUNCTION__;

    this->setWindowIcon(QIcon(":/res/mzlogo.jpg"));
    this->setWindowTitle("主页面管理");

    //初始化一个样式表
    styleName = QStringList{
        "bf",
        "black",
        "blue",
        "darkblack",
        "darkblue",
        "darkgray",
        "flatblack",
        "flatwhite",
        "gray",
        "lightblack",
        "lightblue",
        "lightgray",
        "psblack",
        "red",
        "silvery"
    };

    //主布局容器
    mainLayout = new QVBoxLayout(this);

    //主分割器  Qt::Vertical 表示垂直方向。
    mainSplitter = new QSplitter(Qt::Vertical);

    //创建一个顶部盒子
    topGroupBox = new QGroupBox(this);

    topGroupBox->setStyleSheet("background-color:white;");
    //顶部盒子水平布局
    QHBoxLayout* toplayout = new QHBoxLayout(topGroupBox);

    toplayout->setContentsMargins(0, 0, 0, 0);
    toplayout->setSpacing(0);

    QLabel* labelLogo = new QLabel(this);
    labelLogo->setObjectName("labelLogo");


    labelLogo->setMaximumHeight(40);
    QPixmap logoPix(":/res/mzlogo.jpg");
    labelLogo->setPixmap(logoPix);
    toplayout->addWidget(labelLogo);

    //添加间隔
    toplayout->addSpacing(5);

    //标题栏
    QLabel *labelText = new QLabel("智能门诊挂号系统", this);
    labelText->setMaximumHeight(40);
    labelText->setStyleSheet(
        "background-color: rgb(255, 255, 255);"
        "color: rgb(4, 104, 255);"
        "font: 75 22pt '等线';"
        );
    toplayout->addWidget(labelText);

    toplayout->addSpacing(20);
    // 添加空白间隔，将后面的部件推到右侧
    toplayout->addStretch(40);


    //创建显示时间的标签
    QLabel* dataTimeLabel = new QLabel;
    dataTimeLabel->setMaximumHeight(40);
    dataTimeLabel->setStyleSheet(
        "font: 14pt 'Microsoft YaHei UI';"
        "background-color: rgb(255, 255, 255);"
        "color: rgb(116, 116, 116);"
        );
    QString currentDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    dataTimeLabel->setText(currentDateTime);

    QTimer *timer = new QTimer(this);
    timer->start(1000);
    connect(timer, &QTimer::timeout, [dataTimeLabel]() {
        QString currentDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
        dataTimeLabel->setText(currentDateTime);

    });
    toplayout->addWidget(dataTimeLabel);

    toplayout->addStretch(50);

    //创建下拉列表--背景样式设置
    QComboBox* cbxStyle = new QComboBox(this);
    foreach(auto style,styleName)
    {
        cbxStyle->addItem(style);
    }
    cbxStyle->setFont(QFont("yahei", 13));

    toplayout->addWidget(cbxStyle);

    //连接槽函数，改变样式
    connect(cbxStyle,&QComboBox::currentTextChanged,this,&manageFace::changeStyle);
    toplayout->addStretch(20);

    //换肤
    PeelingBtn = new QPushButton;
    PeelingBtn->setFixedSize(49,49);
    PeelingBtn->setStyleSheet(
        "QPushButton {"
        "border: none;"
        "background-repeat:no-repeat;"
        "background-image: url(:/res/peeling.svg);"
        "}"
        "QPushButton:pressed {"
        "border: none;"
        "background-image: url(:/res/peelingPress.svg);"
        "}"
        "QPushButton:releas {"
        "background-image: url(:/res/peeling.svg);"
        "}"

        );

    //设置
    setBtn = new QPushButton;
    setBtn->setFixedSize(49,49);
    setBtn->setStyleSheet(
        "QPushButton {"
        "border: none;"
        "background-repeat:no-repeat;"
        "background-image: url(:/res/set.png);"
        "}"
        "QPushButton:pressed {"
        "border: none;"
        "background-image: url(:/res/setPress.svg);"
        "}"
        "QPushButton:releas {"
        "background-image: url(:/res/set.png);"
        "}"
        );
    //管理员
    manageBtn = new QPushButton;
    manageBtn->setFixedSize(49,49);
    manageBtn->setStyleSheet(
        "QPushButton {"
        "border: none;"
        "background-repeat:no-repeat;"
        "background-image: url(:/res/manage.png);"
        "}"
        "QPushButton:pressed {"
        "border: none;"
        "background-image: url(:/res/managePress.svg);"
        "}"
        "QPushButton:releas {"
        "background-image: url(:/res/manage.png);"
        "}"
        );
    //关闭
    closeBtn = new QPushButton;
    closeBtn->setFixedSize(49,49);
    closeBtn->setStyleSheet(
        "QPushButton {"
        "border: none;"
        "background-repeat:no-repeat;"
        "background-image: url(:/res/close.svg);"
        "}"
        "QPushButton:hover {"
        "background-image: url(:/res/closeHover.svg);"
        "}"
        "QPushButton:pressed {"
        "background-image: url(:/res/closePress.svg);"
        "}"
        );
    toplayout->addWidget(PeelingBtn);
    toplayout->addStretch(4);
    toplayout->addWidget(setBtn);
    toplayout->addStretch(4);
    toplayout->addWidget(manageBtn);
    toplayout->addStretch(4);
    toplayout->addWidget(closeBtn);
    toplayout->addStretch(4);

    connect(closeBtn,&QPushButton::clicked,this,&manageFace::on_closeBtn_released);

    topGroupBox->setMinimumHeight(55);
    topGroupBox->setMaximumHeight(85);



    //顶部分组框添加到垂直分割器
    mainSplitter->addWidget(topGroupBox);

    //创建下部的分割器
    lrSplitter = new QSplitter(Qt::Horizontal, this);
    //将下部的水平分割器添加到垂直分割器中
    mainSplitter->addWidget(lrSplitter);
    //将垂直分割器添加到主垂直布局中
    mainLayout->addWidget(mainSplitter);

    //下部水平分布
    bottomLayout = new QHBoxLayout(lrSplitter);

    tabWidget=new QTabWidget(this);
    menuWidget=new QTreeWidget(this);
    menuWidget->setFixedWidth(220);
    lrSplitter->addWidget(menuWidget);
    lrSplitter->addWidget(tabWidget);

    setMenuWidget();
    setTabWidget();
}


//根据传入的参数改变样式
void  manageFace::changeStyle(QString styleText)
{


}
// 设置树形菜单部件的相关属性、添加节点等内容，构建菜单结构
void manageFace::setMenuWidget()
{
    qDebug() << __FUNCTION__;

    // 隐藏树形菜单部件的表头
    menuWidget->header()->hide();

    //设置树形菜单表头
    headText = new QTreeWidgetItem(menuWidget);
    headText->setText(0,"首页");
    menuWidget->setHeaderItem(headText);

    allWidgetList["headText"] = (QWidget*)headText;

    //创建一个根节点  患者管理
    {
        QTreeWidgetItem* patientManage = new QTreeWidgetItem(menuWidget);
        patientManage->setText(0,"患者管理");

        //创建子节点
        QTreeWidgetItem* patientRegist = new QTreeWidgetItem(patientManage);
        patientRegist->setText(0,"患者登记");

        //创建子节点
        QTreeWidgetItem* patientList = new QTreeWidgetItem(patientManage);
        patientList->setText(0,"患者列表");

        allWidgetList["patientManage"] = (QWidget*)patientManage;
        allWidgetList["patientRegist"] = (QWidget*)patientRegist;
        allWidgetList["patientList"] = (QWidget*)patientList;
    }



    //创建一个根节点  挂号管理
    {
        QTreeWidgetItem* orderManage = new QTreeWidgetItem(menuWidget);
        orderManage->setText(0,"挂号管理");

        //创建子节点
        QTreeWidgetItem* order = new QTreeWidgetItem(orderManage);
        order->setText(0,"门诊挂号");

        //创建子节点
        QTreeWidgetItem* orderSelect = new QTreeWidgetItem(orderManage);
        orderSelect->setText(0,"挂号查询");

        //创建子节点
        QTreeWidgetItem* blackSelect = new QTreeWidgetItem(orderManage);
        blackSelect->setText(0,"黑名单查询");

        allWidgetList["orderManage"] = (QWidget*)orderManage;
        allWidgetList["order"] = (QWidget*)order;
        allWidgetList["orderSelect"] = (QWidget*)orderSelect;
        allWidgetList["blackSelect"] = (QWidget*)blackSelect;

    }

    //创建一个根节点  排班管理
    {
        QTreeWidgetItem* ScheduManage = new QTreeWidgetItem(menuWidget);
        ScheduManage->setText(0,"排班管理");

        //创建子节点
        QTreeWidgetItem* doctorSch = new QTreeWidgetItem(ScheduManage);
        doctorSch->setText(0,"医生排班");

        //创建子节点
        QTreeWidgetItem* copySch = new QTreeWidgetItem(ScheduManage);
        copySch->setText(0,"复制排班");

        //创建子节点
        QTreeWidgetItem* setSch = new QTreeWidgetItem(ScheduManage);
        setSch->setText(0,"排班设置");

        //创建子节点
        QTreeWidgetItem* schSelect = new QTreeWidgetItem(ScheduManage);
        schSelect->setText(0,"排班查询");

        allWidgetList["ScheduManage"] = (QWidget*)ScheduManage;
        allWidgetList["doctorSch"] = (QWidget*)doctorSch;
        allWidgetList["copySch"] = (QWidget*)copySch;
        allWidgetList["setSch"] = (QWidget*)setSch;
        allWidgetList["schSelect"] = (QWidget*)schSelect;
    }

    //创建一个根节点  统计查询
    {
        QTreeWidgetItem* countSeclect = new QTreeWidgetItem(menuWidget);
        countSeclect->setText(0,"统计查询");

        //创建子节点
        QTreeWidgetItem* patientBuild = new QTreeWidgetItem(countSeclect);
        patientBuild->setText(0,"患者建档");

        //创建子节点
        QTreeWidgetItem* orderSchSelect = new QTreeWidgetItem(countSeclect);
        orderSchSelect->setText(0,"门诊挂号查询");

        allWidgetList["countSeclect"] = (QWidget*)countSeclect;
        allWidgetList["patientBuild"] = (QWidget*)patientBuild;
        allWidgetList["orderSchSelect"] = (QWidget*)orderSchSelect;
    }

    //创建一个根节点  系统管理
    {
        QTreeWidgetItem* systemManage = new QTreeWidgetItem(menuWidget);
        systemManage->setText(0,"系统管理");

        //创建子节点
        QTreeWidgetItem* dictionaryManaged = new QTreeWidgetItem(systemManage);
        dictionaryManaged->setText(0,"字典管理");

        //创建子节点
        QTreeWidgetItem* parameterSet = new QTreeWidgetItem(systemManage);
        parameterSet->setText(0,"参数配置");

        allWidgetList["systemManage"] = (QWidget*)systemManage;
        allWidgetList["dictionaryManaged"] = (QWidget*)dictionaryManaged;
        allWidgetList["parameterSet"] = (QWidget*)parameterSet;
    }

    //创建一个根节点  服务器维护
    {
        QTreeWidgetItem* keepServer = new QTreeWidgetItem(menuWidget);
        keepServer->setText(0,"服务器维护");

        //创建子节点
        QTreeWidgetItem* httpServer = new QTreeWidgetItem(keepServer);
        httpServer->setText(0,"http端");

        //创建子节点
        QTreeWidgetItem* JsonServer = new QTreeWidgetItem(keepServer);
        JsonServer->setText(0,"JSON端");

        //创建子节点
        QTreeWidgetItem* TcpServer = new QTreeWidgetItem(keepServer);
        TcpServer->setText(0,"TCP端");

        //创建子节点
        QTreeWidgetItem* UdpServer = new QTreeWidgetItem(keepServer);
        UdpServer->setText(0,"UDP端");

        allWidgetList["keepServer"] = (QWidget*)keepServer;
        allWidgetList["httpServer"] = (QWidget*)httpServer;
        allWidgetList["JsonServer"] = (QWidget*)JsonServer;
        allWidgetList["TcpServer"] = (QWidget*)TcpServer;
        allWidgetList["UdpServer"] = (QWidget*)UdpServer;
    }

    //当用户点击菜单中的节点时，触发相应的操作
    connect(menuWidget, &QTreeWidget::itemPressed, this, &manageFace::clickMenuWidget);

}


// 设置选项卡部件的相关属性，如样式、关闭功能等
void manageFace::setTabWidget()
{
    //设置选项卡形状为三角形
    tabWidget->setTabShape(QTabWidget::Triangular);

    //点击关闭选项卡
    tabWidget->setTabsClosable(true);

    connect(tabWidget,&QTabWidget::tabCloseRequested,this,&manageFace::closeSubWidget);


}
// 树形菜单部件中节点被点击，根据点击的节点进行操作,新建界面等
void manageFace::clickMenuWidget(QTreeWidgetItem *item, int column)
{
    qDebug() << item->text(column);

    //    //判断点击的是否是根节点
    if(     /*item == (QTreeWidgetItem*)allWidgetList["headText"]      ||*/
            item == (QTreeWidgetItem*)allWidgetList["patientManage"] ||
        item == (QTreeWidgetItem*)allWidgetList["orderManage"]   ||
        item == (QTreeWidgetItem*)allWidgetList["ScheduManage"]  ||
        item == (QTreeWidgetItem*)allWidgetList["countSeclect"]  ||
        item == (QTreeWidgetItem*)allWidgetList["systemManage"]  ||
        item == (QTreeWidgetItem*)allWidgetList["keepServer"]
        ){
        return;
    }
    //首页
    if (item == (QTreeWidgetItem*)allWidgetList["headText"]){
        if (allWidgetList[item->text(column)] == nullptr){
            scrollArea = new QScrollArea(this);
            home *homePage = new home(this);
            scrollArea->setWidget(homePage);
            scrollArea->setWidgetResizable(true);

            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }
    }

    //患者登记管理窗体===============================================
    if(item->text(column)=="患者登记")
    {
        if(allWidgetList[item->text(column)]==nullptr)
        {
            scrollArea = new QScrollArea(this);
            patientregist *pt = new patientregist(this);
            scrollArea->setWidget(pt);
            scrollArea->setWidgetResizable(true);
            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }

    }

    if(item->text(column)=="患者列表")
    {
        if(allWidgetList[item->text(column)]==nullptr)
        {
            scrollArea = new QScrollArea(this);
            patientList *pl = new patientList(this);
            scrollArea->setWidget(pl);
            scrollArea->setWidgetResizable(true);
            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }
    }

    //黑名单查询窗体===============================================
    if(item->text(column)=="黑名单查询")
    {
        if(allWidgetList[item->text(column)]==nullptr)
        {
            scrollArea = new QScrollArea(this);
            blackList *bl = new blackList(this);
            scrollArea->setWidget(bl);
            scrollArea->setWidgetResizable(true);
            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }
    }

    //患者登记管理窗体===============================================
    if(item->text(column)=="门诊挂号")
    {
        if(allWidgetList[item->text(column)]==nullptr)
        {
            scrollArea = new QScrollArea(this);
            registration *rr = new registration(this);
            scrollArea->setWidget(rr);
            scrollArea->setWidgetResizable(true);
            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }
    }


    //挂号查询窗体===============================================
    if(item->text(column)=="挂号查询")
    {
        if(allWidgetList[item->text(column)]==nullptr)
        {
            scrollArea = new QScrollArea(this);
            registSelect *rs = new registSelect();
            scrollArea->setWidget(rs);
            scrollArea->setWidgetResizable(true);
            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }
    }


    //TCP端窗体===============================================
    if(item->text(column)=="TCP端")
    {
        if(allWidgetList[item->text(column)]==nullptr)
        {
            scrollArea = new QScrollArea(this);
            chatWidget *chat = new chatWidget();
            scrollArea->setWidget(chat);
            scrollArea->setWidgetResizable(true);
            allWidgetList[item->text(column)] = scrollArea;
            allWidgetList[item->text(column)]->show();
        }
    }


    if (allWidgetList[item->text(column)] == nullptr)
    {
        // 创建一个新的TestWidget对象（假设TestWidget是自定义的窗口部件类），并传入节点文本作为参数，用于显示对应页面内容
        allWidgetList[item->text(column)] = new testWidget(item->text(column), this);
    }

    // 将对应的部件添加到选项卡部件中，以节点文本作为选项卡的标题显示，并设置为当前选中的选项卡页面
    tabWidget->addTab(allWidgetList[item->text(column)], item->text(column));
    tabWidget->setCurrentWidget(allWidgetList[item->text(column)]);

}


// 关闭选项卡部件中指定索引的页面，并进行相关清理操作
void manageFace::closeSubWidget(int index)
{
    qDebug() << "关闭:" << index;
    tabWidget->setCurrentIndex(index);
    QWidget* currWidget = tabWidget->currentWidget();

    //关闭当前界面
    currWidget->close();

    //从选相卡中移出已关闭的界面
    tabWidget->removeTab(index);
}


//注销登录按钮
void manageFace::on_closeBtn_released(){
    QString message=QString("<h2 style='color:rgb(69,198,214)'>")+" 是否注销登录<br/>";
    QMessageBox::Button  res=   QMessageBox::information(this,"消息",message,QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
    if(res==QMessageBox::Yes){
        this->close();
        loginViewWidget* loginView = new loginViewWidget;
        loginView->show();
    }
}

//管理员图标点击事件
void manageFace::on_manageBtn_clicked()
{
    qDebug() << "管理员图标被点击";
    //     ui->stackedWidget->setCurrentWidget(manageSet);
}

//设置图标点击事件
void manageFace::on_setBtn_clicked()
{
    qDebug() << "设置图标被点击";
    //    ui->stackedWidget->setCurrentWidget(setBtn);
}


// 鼠标按下事件处理函数
void manageFace::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "鼠标点击了";
    QLabel* labelLogo = findChild<QLabel*>("labelLogo");
    if (labelLogo) {
        QRect logoRect = labelLogo->geometry();
        if (logoRect.contains(event->pos())) {
            if (event->button() == Qt::LeftButton) {
                isLogoPressed = true;
                oldPos = event->globalPos() - frameGeometry().topLeft();
            }
        }
    }
    QWidget::mousePressEvent(event);
}


// 鼠标移动事件处理函数
void manageFace::mouseMoveEvent(QMouseEvent *event)
{
    if (isLogoPressed && (event->buttons() & Qt::LeftButton)) {
        move(event->globalPos() - oldPos);
    }
    QWidget::mouseMoveEvent(event);

}
//鼠标松开
void manageFace::mouseReleaseEvent(QMouseEvent *event) {
    isLogoPressed = false;
    QWidget::mouseReleaseEvent(event);
}




